/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.ui.wizards.service;

import static java.awt.GridBagConstraints.CENTER;
import static java.awt.GridBagConstraints.HORIZONTAL;
import static java.awt.GridBagConstraints.NONE;
import static java.awt.GridBagConstraints.VERTICAL;
import static java.awt.GridBagConstraints.WEST;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.isqlviewer.sql.ConnectionProfile;
import org.isqlviewer.sql.JdbcService;
import org.isqlviewer.sql.embedded.EmbeddedDatabase;
import org.isqlviewer.swing.SwingUtilities;
import org.isqlviewer.ui.wizards.AbstractWizardStep;
import org.isqlviewer.ui.wizards.Step;
import org.isqlviewer.ui.wizards.WizardContext;
import org.isqlviewer.util.LocalMessages;

public class SelectServiceFunctionStep extends AbstractWizardStep {

    private LocalMessages messages = new LocalMessages(ServiceWizard.BUNDLE_NAME);
    private ButtonGroup typeSelection = new ButtonGroup();

    private static final List<Class< ? extends AbstractWizardStep>> newServiceWizard;
    private static final List<Class< ? extends AbstractWizardStep>> modifyServiceWizard;
    private static final List<Class< ? extends AbstractWizardStep>> deleteServiceWizard;
    private static final List<Class< ? extends AbstractWizardStep>> importServiceWizard;

    static {
        ArrayList<Class< ? extends AbstractWizardStep>> wizardList = new ArrayList<Class< ? extends AbstractWizardStep>>();
        wizardList.add(ConfigureBasicServiceOptions.class);
        wizardList.add(ConfigureServiceResources.class);
        wizardList.add(ServiceProfileOptions.class);
        wizardList.add(DriverPropertyOptions.class);
        wizardList.add(ConfirmNewService.class);
        newServiceWizard = Collections.unmodifiableList(wizardList);

        wizardList = new ArrayList<Class< ? extends AbstractWizardStep>>();
        wizardList.add(SelectServiceStep.class);
        wizardList.add(ConfigureBasicServiceOptions.class);
        wizardList.add(ConfigureServiceResources.class);
        wizardList.add(ServiceProfileOptions.class);
        wizardList.add(DriverPropertyOptions.class);
        wizardList.add(ConfirmUpdateService.class);
        modifyServiceWizard = Collections.unmodifiableList(wizardList);

        wizardList = new ArrayList<Class< ? extends AbstractWizardStep>>();
        wizardList.add(SelectServiceStep.class);
        wizardList.add(ConfirmDeleteService.class);
        deleteServiceWizard = Collections.unmodifiableList(wizardList);

        wizardList = new ArrayList<Class< ? extends AbstractWizardStep>>();
        importServiceWizard = Collections.unmodifiableList(wizardList);
    }

    public boolean isFirst() {

        return true;
    }

    public boolean isLast() {

        return false;
    }

    public boolean isValid(WizardContext context) {

        if (typeSelection.getSelection() == null) {
            return false;
        }

        String serviceFunction = typeSelection.getSelection().getActionCommand();
        setNextStep(null);
        List<Step> wizardSteps = null;
        if (ServiceWizard.FUNCTION_CREATE.equals(serviceFunction)) {
            wizardSteps = initializeSteps(newServiceWizard, context);
            JdbcService service = new JdbcService();
            service.setProfile(new ConnectionProfile());
            context.setAttribute(ServiceWizard.ATTRIBUTE_SERVICE, service);
        } else if (ServiceWizard.FUNCTION_DELETE.equals(serviceFunction)) {
            wizardSteps = initializeSteps(deleteServiceWizard, context);
        } else if (ServiceWizard.FUNCTION_MODIFY.equals(serviceFunction)) {
            wizardSteps = initializeSteps(modifyServiceWizard, context);
        } else if (ServiceWizard.FUNCTION_IMPORT.equals(serviceFunction)) {
            wizardSteps = initializeSteps(importServiceWizard, context);
        } else {
            return false;
        }

        context.setAttribute(ServiceWizard.ATTRIBUTE_FUNCTION, serviceFunction);
        context.updateSteps(wizardSteps);

        AbstractWizardStep nextStep = (AbstractWizardStep) wizardSteps.get(0);
        nextStep.setPrevious(this);
        setNextStep(nextStep);
        return true;
    }

    @Override
    public void init(WizardContext context) {

        super.init(context);
        setTitle(messages.getMessage("SelectServiceFunctionStep.title"));
        setComment(messages.getMessage("SelectServiceFunctionStep.comment"));
        setImage(SwingUtilities.loadIconResource("question", 22));

        JPanel panel = new JPanel(new GridBagLayout());
        setView(panel);

        GridBagConstraints constraint = null;
        String title = null;
        String tip = null;
        JComponent component = null;
        int serviceCount = 0;

        EmbeddedDatabase edb = EmbeddedDatabase.getSharedInstance();
        try {
            serviceCount = edb.getRegisteredServices().size();
        } catch (SQLException e) {
        }
        title = messages.format("SelectServiceFunction.create-new-service");
        tip = messages.format("SelectServiceFunction.create-new-service.tip");
        component = new JRadioButton();
        typeSelection.add((JRadioButton) component);
        ((JRadioButton) component).setText(title);
        ((JRadioButton) component).setSelected(true);
        ((JRadioButton) component).setActionCommand(ServiceWizard.FUNCTION_CREATE);
        component.setToolTipText(tip);
        constraint = ServiceWizard.constrain(0, 0, 1, 1, 0.0, 0.0, WEST, NONE);
        panel.add(component, constraint);

        title = messages.format("SelectServiceFunction.import-service");
        tip = messages.format("SelectServiceFunction.import.tip");
        component = new JRadioButton();
        typeSelection.add((JRadioButton) component);
        ((JRadioButton) component).setText(title);
        ((JRadioButton) component).setEnabled(false);
        ((JRadioButton) component).setActionCommand(ServiceWizard.FUNCTION_IMPORT);
        component.setToolTipText(tip);
        constraint = ServiceWizard.constrain(0, 1, 1, 1, 0.0, 0.0, WEST, NONE);
        panel.add(component, constraint);

        title = messages.format("SelectServiceFunction.delete-existing-service");
        tip = messages.format("SelectServiceFunction.delete-existing-service.tip");
        component = new JRadioButton();
        typeSelection.add((JRadioButton) component);
        ((JRadioButton) component).setText(title);
        ((JRadioButton) component).setActionCommand(ServiceWizard.FUNCTION_DELETE);
        ((JRadioButton) component).setEnabled(serviceCount > 0);

        component.setToolTipText(tip);
        constraint = ServiceWizard.constrain(0, 2, 1, 1, 0.0, 0.0, WEST, NONE);
        panel.add(component, constraint);

        title = messages.format("SelectServiceFunction.modify-service");
        tip = messages.format("SelectServiceFunction.modify-service.tip");
        component = new JRadioButton();
        typeSelection.add((JRadioButton) component);
        ((JRadioButton) component).setText(title);
        ((JRadioButton) component).setActionCommand(ServiceWizard.FUNCTION_MODIFY);
        ((JRadioButton) component).setEnabled(serviceCount > 0);

        component.setToolTipText(tip);
        constraint = ServiceWizard.constrain(0, 3, 1, 1, 0.0, 0.0, WEST, NONE);
        panel.add(component, constraint);

        constraint = ServiceWizard.constrain(0, 4, 1, 1, 0.0, 1.0, CENTER, VERTICAL);
        panel.add(Box.createVerticalGlue(), constraint);

        constraint = ServiceWizard.constrain(1, 0, 1, 1, 1.0, 0.0, CENTER, HORIZONTAL);
        panel.add(Box.createHorizontalGlue(), constraint);
    }
}